
package fractal;

import java.awt.geom.Rectangle2D;


public class Mandelbrot  extends FractalGenerator {
	// maximum iteration for mandelbrot set
	public static final int MAX_ITERATIONS = 2000;
	
	@Override
	public void getInitialRange(Rectangle2D.Double range) {
		range.x      = -2;
		range.y      = -1.5;
		range.width  = 3;
		range.height = 3;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// unit test
		Mandelbrot m = new Mandelbrot();
		System.out.println("numIterations: " + m.numIterations(0.27, 0.5));

	}

	@Override
	public int numIterations(double x, double y) {
		
		int result = 0;
		
		double initalMagZSquared = magZSquared(x,y);
		double newx, newy, currentx, currenty;
		
		newx = currentx = x;
		newy = currenty = y;
		
		while (initalMagZSquared <= 4.0 && result < MAX_ITERATIONS ) {
			result++;
			newx = currentx * currentx - currenty * currenty + x;
			newy = 2.0 * currentx * currenty + y;
			initalMagZSquared = magZSquared(newx,newy);
			currentx = newx;
			currenty = newy;
		}
		
		if (result == MAX_ITERATIONS)
			return -1;
		else
			return result;
	}

	private double magZSquared(double x, double y) {
		return x * x + y * y;
	}
	
	@Override
	public String toString() {
		return "Mandelbrot";
	}
	
	
}
